<?php
namespace Common\Lib\Validate;
/**
 * 输入验证类
 * Class Validate
 */
class Validate{
    /**
     * @var string 手机号正则表达式
     */
    private static $reg_phone = '/^1[34578]\d{9}$/';
    private static $reg_email = '/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i';
    private static $reg_idCard = '/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/';
    private static $reg_qq = '/[1-9][0-9]{4,}/';
    private static $reg_url = '/^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?$/';
    private static $reg_telephone = '/\d{3}-\d{8}|\d{4}-\d{7}/';

    /**
     * 验证手机号正则表达式
     * @param $phone
     * @return int
     */
    public static function Phone($phone){
        return preg_match(self::$reg_phone,$phone);
    }

    /**
     * 验证座机
     * @param $telephone
     * @return int
     */
    public static function Telephone($telephone){
        return preg_match(self::$reg_telephone,$telephone);
    }

    /**
     * 验证邮箱正则表达式
     * @param $email
     * @return int
     */
    public static function Email($email){
        return preg_match(self::$reg_email,$email);
    }

    /**
     * 验证身份证号
     * @param $idCard
     * @return int
     */
    public static function IdCard($idCard){
        return preg_match(self::$reg_idCard,$idCard);
    }

    /**
     * 验证QQ号
     * @param $qq
     * @return int
     */
    public static function QQ($qq){
        return preg_match(self::$reg_qq,$qq);
    }

    /**
     * 验证URL地址
     * @param $url
     * @return int
     */
    public static function Url($url){
        return preg_match(self::$reg_url,$url);
    }

    /**
     * 验证数字
     * @param $number
     * @param int $min
     * @param int $max
     * @return int
     */
    public static function Number($number,$min = 0,$max = 0){
        if($min){
            if($max){
                if(intval($max) === intval($min)){
                    $preg = '/^\d{'.$min.'}$/';
                    return preg_match($preg,$number);
                }else{
                    $preg = '/^\d{'.$min.','.$max.'}$/';
                    return preg_match($preg,$number);
                }
            }else{
                $preg = '/^\d{'.$min.',}$/';
                return preg_match($preg,$number);
            }
        }
        if($max){
            $preg = '/^\d{0,'.$max.'}$/';
            return preg_match($preg,$number);
        }
        return preg_match('/^[0-9]*$/',$number);
    }

    /**
     * 支持中文(字母、数字和中文)
     * @param $string
     * @param int $min
     * @param int $max
     * @return int
     */
    public static function SupplyChinese($string,$min = 0,$max = 0){
        if($min){
            if($max){
                if(intval($max) === intval($min)){
                    $preg = '/^[a-zA-Z0-9\x{4e00}-\x{9fa5}_\-@&=]{'.$min.'}$/u';
                    return preg_match($preg,$string);
                }else{
                    $preg = '/^[a-zA-Z0-9\x{4e00}-\x{9fa5}_\-@&=]{'.$min.','.$max.'}$/u';
                    return preg_match($preg,$string);
                }
            }else{
                $preg = '/^[a-zA-Z0-9\x{4e00}-\x{9fa5}_\-@&=]{'.$min.',}$/u';
                return preg_match($preg,$string);
            }
        }
        if($max){
            $preg = '/^[a-zA-Z0-9\x{4e00}-\x{9fa5}_\-@&=]{0,'.$max.'}$/u';
            return preg_match($preg,$string);
        }
        return preg_match('/^[a-zA-Z0-9\x{4e00}-\x{9fa5}_\-@&=]+$/u',$string);
    }

    /**
     * 验证是否中文
     * @param $string
     * @param int $min
     * @param int $max
     * @return int
     */
    public static function Chinese($string,$min = 0,$max = 0){
        //$reg_chinese = '/^[\x{4e00}-\x{9fa5}]+$/u';
        if($min){
            if($max){
                if(intval($max) === intval($min)){
                    $preg = '/^[\x{4e00}-\x{9fa5}]{'.$min.'}$/u';
                    return preg_match($preg,$string);
                }else{
                    $preg = '/^[\x{4e00}-\x{9fa5}]{'.$min.','.$max.'}$/u';
                    return preg_match($preg,$string);
                }
            }else{
                $preg = '/^[\x{4e00}-\x{9fa5}]{'.$min.',}$/u';
                return preg_match($preg,$string);
            }
        }
        if($max){
            $preg = '/^[\x{4e00}-\x{9fa5}]{0,'.$max.'}$/u';
            return preg_match($preg,$string);
        }
        return preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$string);
    }

    /**
     * 验证是否英文
     * @param $string
     * @param int $min
     * @param int $max
     * @return int
     */
    public static function English($string,$min = 0,$max = 0){
        if($min){
            if($max){
                if(intval($max) === intval($min)){
                    $preg = '/^[a-zA-Z]{'.$min.'}$/';
                    return preg_match($preg,$string);
                }else{
                    $preg = '/^[a-zA-Z]{'.$min.','.$max.'}$/';
                    return preg_match($preg,$string);
                }
            }else{
                $preg = '/^[a-zA-Z]{'.$min.',}$/';
                return preg_match($preg,$string);
            }
        }
        if($max){
            $preg = '/^[a-zA-Z]{0,'.$max.'}$/';
            return preg_match($preg,$string);
        }
        return preg_match('/^[a-zA-Z]+$/',$string);
    }

    /**
     * 验证用户名(字母开头，字母数字下划线)
     * @param $username
     * @param int $min
     * @param int $max
     * @return int
     */
    public static function UserName($username,$min = 0,$max = 0){
        if($min){
            $min = $min - 1;
            if($max){
                $max = $max - 1;
                if(intval($max) === intval($min)){
                    $preg = '/^[a-zA-Z][a-zA-Z0-9_]{'.$min.'}$/';
                    return preg_match($preg,$username);
                }else{
                    $preg = '/^[a-zA-Z][a-zA-Z0-9_]{'.$min.','.$max.'}$/';
                    return preg_match($preg,$username);
                }
            }else{
                $preg = '/^[a-zA-Z][a-zA-Z0-9_]{'.$min.',}$/';
                return preg_match($preg,$username);
            }
        }
        if($max){
            $max = $max - 1;
            $preg = '/^[a-zA-Z][a-zA-Z0-9_]{0,'.$max.'}$/';
            return preg_match($preg,$username);
        }
        return preg_match('/^[a-zA-Z][a-zA-Z0-9_]+$/',$username);
    }

    /**
     * 常规密码(字母开头，字母数字下划线)
     * @param $password
     * @param int $min
     * @param int $max
     * @return int
     */
    public static function Password($password,$min = 0,$max = 0){
        return self::UserName($password,$min,$max);
    }

    /**
     * 强密码(包含大小写字母，数字，特殊字符)
     * @param $password
     * @param int $min
     * @param int $max
     * @return int
     */
    public static function StrongPassword($password,$min = 0,$max = 0){
        if($min){
            if($max){
                if(intval($max) === intval($min)){
                    $preg = '/^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[_\-@&=])[a-zA-Z0-9_\-@&=]{'.$min.'}$/';
                    return preg_match($preg,$password);
                }else{
                    $preg = '/^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[_\-@&=])[a-zA-Z0-9_\-@&=]{'.$min.','.$max.'}$/';
                    return preg_match($preg,$password);
                }
            }else{
                $preg = '/^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[_\-@&=])[a-zA-Z0-9_\-@&=]{'.$min.',}$/';
                return preg_match($preg,$password);
            }
        }
        if($max){
            $preg = '/^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[_\-@&=])[a-zA-Z0-9_\-@&=]{0,'.$max.'}$/';
            return preg_match($preg,$password);
        }
        return preg_match('/^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[_\-@&=])[a-zA-Z0-9_\-@&=]+$/',$password);
    }

    /**
     * 自定义验证
     * @param $preg
     * @param $string
     * @return int
     */
    public static function Custom($preg,$string){
        return preg_match($preg,$string);
    }




}